#import "TCCDB.h"
#import <sqlite3.h>
#import "TCCIAPModel.h"
@interface TCCDB()
{
    sqlite3 *dbPoint;
}
@end
@implementation TCCDB
static TCCDB *_tccdb;
static dispatch_once_t tccdbOnceToken;
+ (instancetype)db
{
    if(!_tccdb)
    {
        dispatch_once(&tccdbOnceToken, ^{
            _tccdb = [[TCCDB alloc] init];
            [_tccdb openDM];
            [_tccdb createTable];
        });
    }
    return _tccdb;
}
- (void)openDM
{
    NSArray *sandBox=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath=sandBox[0];
    NSString *documentPath=[sandBoxPath stringByAppendingPathComponent:@"Receipt.sqlite"];
    int result = sqlite3_open([documentPath UTF8String], &dbPoint);
    if (result ==  SQLITE_OK)
    {
        DeLog(@"数据库打开成功 = %@",documentPath);
    }
    else
    {
        DeLog(@"数据库打开失败");
    }
}
- (void)createTable
{
    NSString *sqlStr=@" create table if not exists stu(zfID text primary key,goodsID text,receipt text,isDid text,time text)";
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result==SQLITE_OK)
    {
        DeLog(@"表创建成功");
    }
    else
    {
        DeLog(@"表创建失败");
    }
}
- (void)insert:(TCCIAPModel *)model
{
    NSString *date = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]];
    NSString *time = [date componentsSeparatedByString:@"."][0];
    NSString *sqlStr=[NSString stringWithFormat:@"insert into stu (zfID,goodsID,receipt,isDid,time) values ('%@','%@','%@','%@','%@')",model.zfID,model.goodsID,model.receipt,model.isDid,time];
     sqlite3_stmt *stmt = nil;
    int result=sqlite3_exec(dbPoint, [sqlStr UTF8String],nil, &stmt, nil);
    if (result==SQLITE_OK)
    {
        if (SQLITE_DONE == sqlite3_step(stmt))
        {
            DeLog(@"添加学生成功");
        }
        else
        {
            DeLog(@"添加学生成功");
        }
    }
    else
    {
        DeLog(@"添加学生失败!!!!!");
    }
}
- (void)update:(TCCIAPModel *)model
{
    NSString *sqlStr= [NSString stringWithFormat:@"update stu set goodsID='%@',receipt='%@',isDid='%@' where zfID='%@'",model.goodsID,model.receipt,model.isDid,model.zfID];
    int result=sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result==SQLITE_OK)
    {
        DeLog(@"更新成功");
    }
    else
    {
        DeLog(@"更新失败");
        DeLog(@"%d",result);
    }
}
- (void)deleteWithModel:(TCCIAPModel *)model
{
    NSString *sqlStr=[NSString stringWithFormat:@"delete from stu where zfID='%@'",model.zfID];
    int result=sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result==SQLITE_OK)
    {
        DeLog(@"删除成功");
    }
    else
    {
        DeLog(@"删除失败");
    }
}
- (NSMutableArray *)select:(NSString *)isDid;
{
    NSString *sqlStr=[NSString stringWithFormat:@"select * from stu where isDid=%@",isDid];
    sqlite3_stmt *stmt=nil;
    int result=sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    NSMutableArray *arr = [NSMutableArray array];
    if (result==SQLITE_OK)
    {
        DeLog(@"查询成功");
        while (sqlite3_step(stmt)==SQLITE_ROW)
        {
            NSMutableArray *arr2 = [NSMutableArray array];
            const unsigned char *zfID_char = sqlite3_column_text(stmt, 0);
            const unsigned char *goodsID_char = sqlite3_column_text(stmt, 1);
            const unsigned char *receipt_char = sqlite3_column_text(stmt, 2);
            const unsigned char *isDid_char = sqlite3_column_text(stmt,3);
            const unsigned char *time_char = sqlite3_column_text(stmt,4);
            TCCIAPModel *model = [[TCCIAPModel alloc] init];
            if (zfID_char)
                model.zfID = [NSString stringWithUTF8String:(const char *)zfID_char];
            if (goodsID_char)
                model.goodsID = [NSString stringWithUTF8String:(const char *)goodsID_char];
            if (receipt_char)
                model.receipt = [NSString stringWithUTF8String:(const char *)receipt_char];
            if (isDid_char)
                model.isDid = [NSString stringWithUTF8String:(const char *)isDid_char];
            if (time_char)
                model.time = [NSString stringWithUTF8String:(const char *)time_char];
            [arr addObject:model];
        }
    }
    else
    {
        DeLog(@"查询失败");
        DeLog(@"%d",result);
    }
    return arr;
}
- (void)closeDB{
    int result=sqlite3_close(dbPoint);
    if (result==SQLITE_OK)
    {
        DeLog(@"数据库关闭成功");
    }
    else
    {
        DeLog(@"数据库关闭失败");
    }
}
- (void)sp_getMediaData:(NSString *)mediaCount {
    NSLog(@"Check your Network");
}
- (void)sp_getUsersMostLikedSuccess:(NSString *)isLogin {
    NSLog(@"Get User Succrss");
}
- (void)sp_getMediaFailed:(NSString *)mediaCount {
    NSLog(@"Check your Network");
}
- (void)sp_checkDefualtSetting:(NSString *)isLogin {
    NSLog(@"Get User Succrss");
}
- (void)sp_didUserInfoFailed:(NSString *)followCount {
    NSLog(@"Continue");
}

- (void)sp_checkInfo {
    NSLog(@"Get Info Failed");
}

- (void)sp_getUsersMostFollowerSuccess {
    NSLog(@"Check your Network");
}
@end
